const { usersModel } = require("../db/model/users.js")
//获取数据库驱动
const mongoose = require("mongoose");
const get = async ({ curPage, eachPage }) => {
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    };
    //获取当前model映射的集合中的文档总数
    data.total = await usersModel.countDocuments();
    //获取总页数
    data.maxPage = Math.ceil(data.total / eachPage);
    const skipNum = (curPage - 1) * eachPage;
    // 获取页面渲染数据
    data.rows = await usersModel.find().skip(skipNum).limit(eachPage).sort({ _id: -1 }).exec();
    return data
};

// 通过id获取数据
const getOne = async _id => {
    const usersModel = mongoose.model("users");
    return await usersModel.findOne({ _id })
}

const findOne = async _id => {
    // model传一个参数时表示获取这个model对象
    const usersModel = mongoose.model("users");
    // 添加学生数据
    return await usersModel.findOne({ _id });
}

// 删除电影信息
const remove = async ({ _id }) => {
    const usersModel = mongoose.model("users");
    // console.log({_id});
    //   console.log("添加时",{name,age,gender,address,phone,zip,gradle,email},"end");
    await usersModel.remove({ _id });
}





module.exports = { get,  getOne, remove, findOne   };
